package com.luoshenshen.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

/**
 * @author ShenshenLuo
 * @date 2025/4/10 23:24
 **/
public class NettyBuf {
    public static void main(String[] args) {
        ByteBuf buf = Unpooled.buffer(1024);

        for (int i = 0; i < 1024; i++) {
            buf.writerIndex(i);
        }

        System.out.println(buf.capacity());
        for (int i = 0; i < 1024; i++) {
            System.out.println(buf.readerIndex(i));
        }

        ByteBuf byteBuf = Unpooled.copiedBuffer("hello", CharsetUtil.UTF_8);
        if (byteBuf.hasArray()) {
            byte[] bytes = byteBuf.array();
            System.out.println(byteBuf.readableBytes());
            System.out.println(bytes.length);
            byte[] newBytes = new byte[byteBuf.readableBytes()];
            for (int i = 0; i < byteBuf.readableBytes(); i++) {
                newBytes[i] = byteBuf.readByte();
            }
            System.out.println(new String(newBytes, CharsetUtil.UTF_8));
        }

    }
}
